package com.rm.zeyou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rm.zeyou.constant.NumberConstants;
import com.rm.zeyou.entity.PayoutConfig;
import com.rm.zeyou.entity.Technician;
import com.rm.zeyou.entity.User;
import com.rm.zeyou.mapper.PayoutConfigMapper;
import com.rm.zeyou.mapper.TechnicianBackMapper;
import com.rm.zeyou.mapper.UserBackMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.PayoutConfigService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.response.SelectPayoutWayResponseVo;
import com.rm.zeyou.vo.response.TechnicianBackResponseVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 提现方式配置表 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-03-31 15:27:22
 */
@Service
public class PayoutConfigServiceImpl extends ServiceImpl<PayoutConfigMapper, PayoutConfig> implements PayoutConfigService {
    @Resource
    private PayoutConfigMapper payoutConfigMapper;
    @Resource
    private UserBackMapper userBackMapper;
    @Resource
    private TechnicianBackMapper technicianBackMapper;

    @Override
    public ApiRestResponse<List<SelectPayoutWayResponseVo>> selectPayoutWay(User user) {
        List<SelectPayoutWayResponseVo> responseVoList = new ArrayList<>();
        List<PayoutConfig> payoutConfigs = payoutConfigMapper.selectList(new LambdaQueryWrapper<PayoutConfig>()
                .eq(PayoutConfig::getEnable, NumberConstants.ONE_CONSTANTS)
                .orderByAsc(PayoutConfig::getSort));
        if (!CheckEmptyUtil.isEmpty(payoutConfigs)) {
            for (PayoutConfig payoutConfig : payoutConfigs) {
                SelectPayoutWayResponseVo responseVo = new SelectPayoutWayResponseVo();
                responseVo.setId(payoutConfig.getId());
                responseVo.setUserBackId(0L);
                responseVo.setIcon(payoutConfig.getIcon());
                responseVo.setPayoutName(payoutConfig.getName());
                responseVoList.add(responseVo);
            }
        }

        List<TechnicianBackResponseVo> technicianBacks = userBackMapper.selectUserBackList(user.getId());
        if (!CheckEmptyUtil.isEmpty(technicianBacks)) {
            for (TechnicianBackResponseVo technicianBack : technicianBacks) {
                SelectPayoutWayResponseVo responseVo = new SelectPayoutWayResponseVo();
                responseVo.setId(4L);
                responseVo.setUserBackId(technicianBack.getId());
                responseVo.setIcon(technicianBack.getBackLogo());
                responseVo.setPayoutName(technicianBack.getBackName());
                responseVoList.add(responseVo);
            }
        }
        if (!CheckEmptyUtil.isEmpty(responseVoList)) {
            responseVoList = responseVoList.stream()
                    .sorted(Comparator.comparing(SelectPayoutWayResponseVo::getId))
                    .filter(responseVo -> responseVo.getUserBackId() != 0 || responseVo.getId() != 1)
                    .collect(Collectors.toList());
        }
        return ApiRestResponse.success(responseVoList);
    }

    @Override
    public ApiRestResponse<List<SelectPayoutWayResponseVo>> selectTechnicianPayoutWay(Technician technician) {
        List<SelectPayoutWayResponseVo> responseVoList = new ArrayList<>();
        List<PayoutConfig> payoutConfigs = payoutConfigMapper.selectList(new LambdaQueryWrapper<PayoutConfig>()
                .eq(PayoutConfig::getEnable, NumberConstants.ONE_CONSTANTS)
                .orderByAsc(PayoutConfig::getSort));
        if (!CheckEmptyUtil.isEmpty(payoutConfigs)) {
            for (PayoutConfig payoutConfig : payoutConfigs) {
                SelectPayoutWayResponseVo responseVo = new SelectPayoutWayResponseVo();
                responseVo.setId(payoutConfig.getId());
                responseVo.setUserBackId(0L);
                responseVo.setIcon(payoutConfig.getIcon());
                responseVo.setPayoutName(payoutConfig.getName());
                responseVoList.add(responseVo);
            }
        }

        List<TechnicianBackResponseVo> technicianBacks = technicianBackMapper.selectTechnicianBackList(technician.getId());
        if (!CheckEmptyUtil.isEmpty(technicianBacks)) {
            for (TechnicianBackResponseVo technicianBack : technicianBacks) {
                SelectPayoutWayResponseVo responseVo = new SelectPayoutWayResponseVo();
                responseVo.setId(4L);
                responseVo.setUserBackId(technicianBack.getId());
                responseVo.setIcon(technicianBack.getBackLogo());
                responseVo.setPayoutName(technicianBack.getBackName());
                responseVoList.add(responseVo);
            }
        }
        if (!CheckEmptyUtil.isEmpty(responseVoList)) {
            responseVoList = responseVoList.stream()
                    .sorted(Comparator.comparing(SelectPayoutWayResponseVo::getId))
                    .filter(responseVo -> responseVo.getUserBackId() != 0 || responseVo.getId() != 1)
                    .collect(Collectors.toList());
        }
        return ApiRestResponse.success(responseVoList);
    }
}
